package com.example.controller;

import com.example.annotation.LogOperation;
import com.example.entity.Dept;
import com.example.service.DeptService;
import com.example.util.Result;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    
    @Autowired
    private DeptService deptService;

    @PostMapping
    public Result save(@RequestBody Dept dept) {
//        System.out.println("新增部门信息：" + dept);
        log.info("新增部门信息，{}", dept);
        deptService.save(dept);
        return Result.success();
    }

    @LogOperation
    @GetMapping
    public Result list() {
        log.info("查询部门列表数据");
        List<Dept> depts = deptService.findAll();
        return Result.success(depts);
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
//        System.out.println("根据路径的ID查询：" + id);
        log.info("根据id查询，id: {}", id);
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept) {
//        System.out.println("更新部门信息：" + dept);
        log.info("修改部门信息，dept:{}", dept);
        deptService.update(dept);
        return Result.success();
    }
//    @DeleteMapping("/depts")
//    public Result delete(HttpServletRequest request) {
//        String idStr = request.getParameter("id");
//        int id = Integer.parseInt(idStr);
//        System.out.println("根据ID删除部门：" + id);
//        return Result.success();
//    }
    @DeleteMapping
    public Result delete(@RequestParam("id") Integer id) {
        log.info("根据部门id对部门进行删除，ID = {}", id);
        System.out.println("根据ID删除部门：" + id);
        return Result.success();
    }
}